package provider.gateway;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import libra.model.JsonResult;

/**
 * 网关API - GateWay专用测试Filter过滤器Controller
 * @author libra
 */
@RestController
@RequestMapping("gw/filter")
public class GatewayFilterController extends BaseController {
	@Value( "#{'${server.servlet.context-path}'.concat('[port(${server.port})]')}" )
	String identity;
	
	private JsonResult<Object> buildSuccResp( Object obj ){
		return new JsonResult<Object>(0, identity, obj);
	}
	
	// 测试Gateway.Filter的 -AddRequestHeader过滤器，通过Gateway向当前请求添加请求头
	@RequestMapping("addRequestHeader")
	public Object addRequestHeader( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	// 测试Gateway.Filter的 -AddRequestHeader过滤器，通过Gateway向当前请求动态【REST解析】添加请求头
	@RequestMapping("addRequestHeader/{p}")
	public Object addRequestHeaderForRest( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	
	// 测试Gateway.Filter的 -AddRequestParameter过滤器，通过Gateway向当前请求添加请求参数
	@RequestMapping("addRequestParameter")
	public Object addRequestParameter( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allParams", getAllParams(req));
		}} );
	}
	// 测试Gateway.Filter的 -AddRequestParameter过滤器，通过Gateway向当前请求动态【REST解析】添加请求参数
	@RequestMapping("addRequestParameter/{p}")
	public Object addRequestParameterForRest( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allParams", getAllParams(req));
		}} );
	}
	
	// 测试Gateway.Filter的 -AddResponseHeader过滤器，通过Gateway向当前响应中添加请求参数
	@RequestMapping("addResponseHeader")
	public Object addResponseHeader( HttpServletRequest req ) throws Exception{
		return buildSuccResp( null );
	}
	// 测试Gateway.Filter的 -AddRequestParameter过滤器，通过Gateway向当前响应动态【REST解析】添加响应头
	@RequestMapping("addResponseHeader/{p}")
	public Object aaddResponseHeaderForRest( HttpServletRequest req ) throws Exception{
		return buildSuccResp( null );
	}
	
	// 测试Gateway.Filter的 -RemoveResponseHeader过滤器，通过Gateway从当前请求移除请求头
	@RequestMapping("removeRequestHeader")
	public Object removeRequestHeader( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	
	// 测试Gateway.Filter的 -RemoveRequestParameter过滤器，通过Gateway从当前请求移除请求参数
	@RequestMapping("removeRequestParameter")
	public Object removeRequestParameter( HttpServletRequest req ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allParams", getAllParams(req));
		}} );
	}
	
	// 测试Gateway.Filter的 -RemoveResponseHeader过滤器，通过Gateway从当前响应中移除响应头
	@RequestMapping("removeResponseHeader")
	public Object removeResponseHeader( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		resp.setHeader("self-filter-resp-header", "Response_Header");
		return buildSuccResp( null );
	}
	
	// 测试Gateway.Filter的 -SetRequestHeader过滤器，通过Gateway从当前请求中修改请求头
	@RequestMapping("setRequestHeader")
	public Object setRequestHeader( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	// 测试Gateway.Filter的 -SetRequestHeader过滤器，通过Gateway从当前请求中基于【REST解析】修改请求头
	@RequestMapping("setRequestHeader/{p}")
	public Object setRequestHeaderForRest( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("allHeaders", getAllHeaders(req));
		}} );
	}
	
	// 测试Gateway.Filter的 -SetRequestHeader过滤器，通过Gateway从当前请求中修改请求头
	@RequestMapping("setResponseHeader")
	public Object setResponseHeader( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		resp.setHeader("self-filter-resp-header", "Response_Header");
		return buildSuccResp( null );
	}
	// 测试Gateway.Filter的 -SetRequestHeader过滤器，通过Gateway从当前请求中基于【REST解析】修改请求头
	@RequestMapping("setResponseHeader/{p}")
	public Object setResponseHeaderForRest( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		resp.setHeader("self-filter-resp-header1", "Response_Header");
		return buildSuccResp( null );
	}
	
	// 测试Gateway.Filter的 -SetStatus过滤器，通过Gateway修改当前请求的请求状态码
	@RequestMapping("setStatus")
	public Object setStatus( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		resp.setStatus(555);
		return buildSuccResp( null );
	}
	
	// 测试Gateway.Filter的 -RedirectTo过滤器，通过Gateway设置当前请求的重定向
	@RequestMapping("redirectTo")
	public Object redirectTo( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		resp.setStatus(666);
		return buildSuccResp( null );
	}
	
	// 测试Gateway.Filter的 -PrefixPath过滤器，通过Gateway添加请求前缀
	@RequestMapping("prefixPath")
	public Object prefixPath( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( "prefixPath" );
	}
	// 测试Gateway.Filter的 -StripPrefix过滤器，通过Gateway截断请求前缀
	@RequestMapping("stripPrefix")
	public Object stripPrefix( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( "stripPrefix" );
	}
	
	
	// 测试Gateway.Filter的 -StripPrefix过滤器，通过Gateway截断请求前缀
	@RequestMapping("selfFilter")
	public Object selfFilter( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("请求路径", req.getRequestURI());
			put("allHeaders", getAllHeaders(req));
			put("allParamS", getAllParams(req));
		}} );
	}
	// 测试Gateway.Filter的 -StripPrefix过滤器，通过Gateway截断请求前缀
	@RequestMapping("selfFilter2")
	public Object selfFilter2( HttpServletRequest req, HttpServletResponse resp ) throws Exception{
		return buildSuccResp( new HashMap<>() {{
			put("请求路径", req.getRequestURI());
			put("allHeaders", getAllHeaders(req));
			put("allParamS", getAllParams(req));
		}} );
	}
}
